import json
import time
import requests
import os
from bs4 import BeautifulSoup

base_path='datas/'

def get_word_in_net(url,word={}):
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
    }
    res = requests.get(url, headers=headers)

    soup = BeautifulSoup(
        res.text,
        'html.parser'
    )

    word_text = soup.find('div', class_='word-text').get_text().strip()  # 单词
    word_pronounce = soup.find('div', class_='pronounces').get_text().strip()  # 发音
    word_simple = soup.find('div', class_='simple').get_text().strip()  # 词性和解释
    word_detail = ''
    temp = soup.select('section.detail-groups p ')
    for item in temp:
        word_detail += item.get_text().strip() + '\n'

    word['word_text'] = word_text
    word['word_pronounce'] = word_pronounce
    word['word_simple'] = word_simple
    word['word_detail'] = word_detail
    word['update_time'] = time.time()
    return word


def read_json_file(path):
    file=open(path)
    pyobj=json.load(file)
    return pyobj

def write_json_file(path,pyobj):
    file = open(path, 'w')
    json.dump(pyobj, file)


def add_time_by_hour(ticks,h):
    ticks+= 60 * 60 * 1
    return ticks

def add_time_by_day(ticks,d):
    ticks+= 60 * 60 * 1*24
    return ticks


def show_front(word):
    print(word['word_text'])

def show_word(word):
    print(word['word_text'])
    print(word['word_pronounce'])
    print(word['word_simple'])
    print(word['word_detail'])

def show_word_all(word):
    print(word['word_text'])
    print(word['word_pronounce'])
    print(word['word_simple'])
    print(word['word_detail'])
    print('上一次更新时间: ',time.asctime(time.localtime(word['update_time'])))
    print('下次出现时间: ',time.asctime(time.localtime(word['next_time'])))


def load_word(word_text):
    word= read_json_file(base_path +word_text)
    return word

def add_new_word(word_text):
    word = {}
    word['word_text'] = word_text
    word['word_pronounce'] =""
    word['word_simple'] = ""
    word['word_detail'] = ""
    word['update_time'] = time.time()
    word['next_time']=time.time()
    write_json_file(base_path+word_text,word)

def update(word):
    write_json_file(base_path+word['word_text'],word)

def get_fx_list(folder_path=base_path):
    ticks=time.time()
    fx_list=[]
    file_name_list=os.listdir(folder_path)
    for file_name in file_name_list:
        word=read_json_file(folder_path+file_name)
        if ticks>=word['next_time']:
            fx_list.append(word)
    return fx_list



# word=get_word_in_net("https://dict.hjenglish.com/jp/jc/すき")
#
# write_json_file(path='datas/'+word['word_text'],pyobj=word)
# x=read_json_file(path='datas/'+word['word_text'])
# show_word(x)

# add_new_word('好き')
# word=read_json_file('datas\好き')
# show_word(word)


# add_new_word('ばか')
# word=read_json_file(base_path+'ばか')
# word['next_time']=add_time_by_hour(word['next_time'],1)
# word=get_word_in_net('https://dict.hjenglish.com/jp/jc/%E3%81%B0%E3%81%8B',word)
# update(word)
# word=load_word(word['word_text'])
# show_word_all(word)

# fx_list=get_fx_list()
# print(fx_list)